package ar.edu.unq.sandsoft.constructors;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import ar.edu.unq.sandsoft.exceptions.CouldnFindAttribute;
import ar.edu.unq.sandsoft.utils.SandFile;

/**
 *  
 */
public class SandFileConfigReader extends AbstractSandReader {
    private static final String RESOURCE = "resource";

    private static final String MAPPING = "mapping";

    private static final String SERVER = "server";

    private static final String CONFIG_XML = "sand.config.xml";

    public SandFileConfigReader() {
        super(new SandFile(CONFIG_XML));
    }

    /**
     * Carga la configuracion de SandSoft
     * 
     * @return
     */
    public List<String> loadConfiguration() throws SAXException, IOException, ParserConfigurationException,
            CouldnFindAttribute {
        this.glance();

        List<String> resources = new ArrayList<String>();

        NodeList maps = this.getAssociatedDocument().getElementsByTagName(MAPPING);

        for (int index = 0; index < maps.getLength(); index++) {
            resources.add(findAttribute(RESOURCE, maps.item(index)));
        }

        return resources;
    }

    public String loadServer() {
        return this.getAssociatedDocument().getElementsByTagName(SERVER).item(0).getAttributes().getNamedItem(NAME)
                .getNodeValue();
    }
}
